package cn.pku.sirx.snad.uias.analyzer;

import org.springframework.util.Assert;

import cn.pku.sirx.snad.uias.analyzer.similarity.ISimilarityAnalyzer;
import cn.pku.sirx.snad.uias.dao.IUserDao;
import cn.pku.sirx.snad.uias.model.entity.SiteUser;
import cn.pku.sirx.snad.uias.util.AnalyzerUtils;
import cn.pku.sirx.util.MathUtils;

/**
 * @author WangXiao
 */
public class UserSimilarityAnalyzer implements IAnalyzer {

	public UserSimilarityAnalyzer() {
		weightVector = MathUtils.standardizeVector(weightVector);
	}

	@Override
	public double analyze(String fromUserId, String toUserId) {
		Assert.notNull(fromUserId);
		Assert.notNull(toUserId);

		/*
		 * init
		 */
		SiteUser fromUser = userDao.get(fromUserId);
		if (fromUser == null) {
			return .0;
		}
		SiteUser toUser = userDao.get(toUserId);
		if (toUser == null) {
			return .0;
		}
		/*
		 * calculate value vector
		 */
		double[] valueVector = new double[] {
				genderAnalyzer.analyze(fromUser, toUser),
				areaAnalyzer.analyze(fromUser, toUser),
				schoolAndProfessionAnalyzer.analyze(fromUser, toUser),
				classAnalyzer.analyze(fromUser, toUser),
				interestingAnalyzer.analyze(fromUser, toUser) };
		return AnalyzerUtils.analyzeByWeight(weightVector, valueVector);
	}

	private double[] weightVector = { 10, 5, 3, 3, 8 };

	private ISimilarityAnalyzer genderAnalyzer;
	private ISimilarityAnalyzer areaAnalyzer;
	private ISimilarityAnalyzer schoolAndProfessionAnalyzer;
	private ISimilarityAnalyzer classAnalyzer;
	private ISimilarityAnalyzer interestingAnalyzer;

	private IUserDao userDao;

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setGenderAnalyzer(ISimilarityAnalyzer genderAnalyzer) {
		this.genderAnalyzer = genderAnalyzer;
	}

	public void setAreaAnalyzer(ISimilarityAnalyzer areaAnalyzer) {
		this.areaAnalyzer = areaAnalyzer;
	}

	public void setSchoolAndProfessionAnalyzer(
			ISimilarityAnalyzer schoolAndProfessionAnalyzer) {
		this.schoolAndProfessionAnalyzer = schoolAndProfessionAnalyzer;
	}

	public void setClassAnalyzer(ISimilarityAnalyzer classAnalyzer) {
		this.classAnalyzer = classAnalyzer;
	}

	public void setInterestingAnalyzer(ISimilarityAnalyzer interestingAnalyzer) {
		this.interestingAnalyzer = interestingAnalyzer;
	}

}
